<!DOCTYPE html>
<HTML>
<HEAD>
	<title>权限配置</title>
	<meta charset="utf-8">
    <script type="text/javascript" charset="UTF-8" src="../../../../static/js/header_base.js"></script>
    <script type="text/javascript" charset="UTF-8" src="../../../../static/js/header_ztree.js"></script>
	<script type="text/javascript" charset="UTF-8" src="../../../../static/js/header_com.js"></script>	
	<script type="text/javascript">	
		var layer;var ztreeObj;var treeNode =[];
		layui.config({
			base : "../../../../static/js/"
		}).extend({
			"application" : "application",
			"publicUtil" : "publicUtil"
		})
		layui.use(['layer','application',"publicUtil",'application'],function(){
			var form = layui.form;
				layer = layui.layer,
				application = layui.application,
				publicUtil  = layui.publicUtil,
				_$ = layui.jquery,
				layer =layui.layer;
		

			//ztree设置
			var setting = {
					view: {
							selectedMulti: false
					},
					check: {
							enable: true
					},
					data: {
							simpleData: {
									enable: true,
									idKey: "id",
									pIdKey: "parentId"
									// rootPId: 0									
							}
					}
			};

			//获取该角色已经配置的权限
			
			function getTreeCheckDatas(){
				var treeSelectDatas;

			}
			$(document).ready(function(){
				$("#reset").click(function(){
					ztreeObj.checkAllNodes(false);
				});
				
				_$.ajax({
					url:  application.SERVE_URL+'/sys/sysmenu/list',                  //ajax请求地址
					success: function (res) {
							ztreeObj = $.fn.zTree.init($("#tree"), setting,res.data);  //加载数据
							var nodeList = ztreeObj.getNodes();
				　　　　　　//展开第一个根节点
							ztreeObj.expandNode(nodeList[0], true);
							// getTreeCheckDatas(ztreeObj);
							publicUtil.setTreeSel(parent.formdatas.menuList,ztreeObj);
					}
				});
					
				// getTreeCheckDatas()	;
			});
			
			
			form.on("submit(getSelectData)",function(data){
				//弹出loading				
				if(getSelectData(ztreeObj.getCheckedNodes()).length === 0 ){					
					layer.msg("请至少选择一个菜单");
					return  false ;
				}else{
					var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
					_$.ajax({
						url: application.SERVE_URL+"/sys/sysrole/saveRoleMenu", //ajax请求地址
						contentType: "application/json",
						data:JSON.stringify({
							id : parent.formdatas.id,
							menuList : getSelectData(ztreeObj.getCheckedNodes())
						}),			
						success: function (res) {
							
							if(res.code==application.REQUEST_SUCCESS){
								top.layer.close(index);
								layer.msg("权限修改成功");
								//刷新父页面
								parent.location.reload();	
							}else{
								layer.msg("权限修改失败！");
							}
						}
					});	
				}	
				return false;
			})
			
		    		
		});	

		function getSelectData(treeCheckedDatas){
			 var menuIds =new Array();
			 for(var i =0;i<treeCheckedDatas.length;i++){
				 menuIds.push({"id" : treeCheckedDatas[i].id}); 
			 }
			 return menuIds;
		}
		 //转换数据
		 function covert(data){
			 var nodes =new Array();
			 for(var i =0;i<data.length;i++){
				 nodes.push({id: data[i].id, pId: data[i].parentId,name :data[i].name}); 
			 }
			 return nodes;
		 }
	</script>
</HEAD>
<BODY class="childrenBody">
	<form class="layui-form border">
		<div style="margin-bottom: 30px;">
			<ul id="tree" class="ztree"></ul>
		</div>
		<div class="layui-form-item layui-row layui-col-xs12" style="float:right;position:fixed;">	
		</div>
		<div class="layui-input-block" style="position:fixed;top:90%;width:100%;margin-left:-12px;background:#f8f8f8;text-align:center;">
			<a class="layui-btn layui-btn-sm" lay-submit="" lay-filter="getSelectData">确认</a>
			<a  class="layui-btn layui-btn-sm layui-btn-primary" id="reset">重置</a>
		</div>
	</form>
</BODY>
</HTML>